package com.sun.service;/**
 * @author sun
 * @date 2022/1/4 - 17:05
 */

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @description: .anyRequest().authenticated();自定义拦截
 * @author 孙
 * @date 2022/1/4 17:05
 * @version 1.0
 */
@Service
public class MyServiceImpl implements MyService {
    @Override
    public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails){
           UserDetails userDetails=(UserDetails) principal;
             Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
             //GrantedAuthority的实现类SimpleGrantedAuthority
            //放行登陆时的赋权，请求，角色，
             return authorities.contains(new SimpleGrantedAuthority(request.getRequestURI()));
        }
        return false;
    }
}
